package cloud.maque.admin.controller;

import cloud.maque.admin.annotations.MaquerManager;
import cloud.maque.admin.domain.entity.MqTenant;
import cloud.maque.admin.domain.vo.TenantSimple;
import cloud.maque.admin.service.MqTenantService;
import cloud.maque.framework.common.request.PaginateDataResquest;
import cloud.maque.framework.common.response.ResponseBodyVo;
import cloud.maque.framework.security.ssoclient.utils.SsoUserInfoUtils;
import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 租户接口
 *
 * @author zhuyf
 * @date 2023/3/17 20:57
 */
@RestController
@RequestMapping(value = "/tenant")
@Api(tags = "运维-租户接口")
public class TenantController {


    @Autowired
    MqTenantService tenantService;

    @Autowired
    SsoUserInfoUtils infoUtils;


    @PostMapping("/list")
    @ApiOperation(value = "查询")
    @MaquerManager
    public ResponseBodyVo<PageInfo<MqTenant>> page(@RequestBody @ApiParam("查询内容") PaginateDataResquest<MqTenant> pageReq) {
        return ResponseBodyVo.Success(tenantService.page(pageReq));
    }

    @GetMapping("/detail")
    @ApiOperation(value = "详情")
    public ResponseBodyVo<MqTenant> Detail(@RequestParam("id") String tenantId) {
        return ResponseBodyVo.Success(tenantService.getById(tenantId));
    }

    @GetMapping("/detailByCodeSimple/{code}")
    @ApiOperation(value = "根据Code查找数据，并返回简单数据")
    public ResponseBodyVo<TenantSimple> detailByCodeSimple(@PathVariable("code") String tenantCode) {
        MqTenant tenant = tenantService.getByCode(tenantCode);
        if (tenant != null) {
            if (tenant.getDisabled() != null && tenant.getDisabled().equals(1)) {
                return ResponseBodyVo.Error("租户已禁用");
            }

            if (tenant.getExpireTime() == null || tenant.getExpireTime().compareTo(new Date()) < 0) {
                return ResponseBodyVo.Error("租户期限已过期,请尽快续费！");
            }

            TenantSimple tenantSimple = new TenantSimple();
            BeanUtil.copyProperties(tenant, tenantSimple);
            return ResponseBodyVo.Success(tenantSimple);
        } else {
            return ResponseBodyVo.Error("租户不存在");
        }

    }

    @PostMapping("/save")
    @ApiOperation(value = "新增或修改")
    public ResponseBodyVo<MqTenant> save(@RequestBody @ApiParam("内容") MqTenant mqTenant) {

        return ResponseBodyVo.Success(tenantService.saveTenant(mqTenant));
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除")
    @MaquerManager
    public ResponseBodyVo<Boolean> Delete(@RequestParam("id") String tenantId) {
        return ResponseBodyVo.Success(tenantService.removeById(tenantId));
    }

    @GetMapping("/detailByUser")
    @ApiOperation(value = "根据用户会话读取")
    public ResponseBodyVo<MqTenant> detailByUser() {
        String tenantId = infoUtils.getTenantId();
        return ResponseBodyVo.Success(tenantService.getById(tenantId));

    }


}
